package com.xishu.util;

import com.xishu.exception.ResponseStatusException;
import gui.ava.html.image.generator.HtmlImageGenerator;
import org.fit.cssbox.demo.ImageRenderer;
import org.fit.cssbox.layout.Dimension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;

/**
 * html to img
 */
public class HtmlToImgUtil {
    private static Logger logger = LoggerFactory.getLogger(HtmlToImgUtil.class);

    public static File htmlToImg(String html) throws ResponseStatusException {
        HtmlImageGenerator imageGenerator = new HtmlImageGenerator();

        //加载html模版
        imageGenerator.loadHtml(html);

        File tmpFile = MongoId.getInstance().generateTmpFile("png");
        //把html写入到图片
        imageGenerator.saveAsImage(tmpFile);

        return tmpFile;
    }

    public static File newHtmlToImg(String html) {
        try {
            ImageRenderer render = new ImageRenderer();
            render.setWindowSize(new Dimension(1200, 800), false);
            File tmpFile = MongoId.getInstance().generateTmpFile("png");
            FileOutputStream out = new FileOutputStream(tmpFile);
            render.renderURL("http://cssbox.sourceforge.net/download.php", out);
            return tmpFile;
        } catch (Exception e) {
            logger.error("e", e);
        }

        return null;
    }
}
